package com.cai.oamanager.controller;

import com.cai.oamanager.entity.Message;
import com.cai.oamanager.entity.PageUtil;
import com.cai.oamanager.entity.People;
import com.cai.oamanager.service.MessageService;
import com.cai.oamanager.service.UserService;
import com.cai.oamanager.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/18 22 : 05
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @RequestMapping("sendmessage.html")
    public String sendMessage(HttpServletRequest request, Model model,@RequestParam(defaultValue = "") String reciver,@RequestParam(defaultValue = "1") Integer currentPage){
        System.out.println("reciver>>>"+reciver);
        People people = (People) request.getSession().getAttribute("people");
        String oid = people.getOid();
        Integer sendCountMessage = messageService.getSendCountMessage(oid);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setSize(5);
        pageUtil.setTotal(sendCountMessage);
        pageUtil.setCurrentPage(currentPage);
        pageUtil.startPage();
        pageUtil.totalNumPage();
        System.out.println(reciver+" ** "+oid+" ** "+pageUtil.getStart()+" ** "+ pageUtil.getSize());
        List<Message> sendMessageList = messageService.getSendMessage(reciver, oid, pageUtil.getStart(), pageUtil.getSize());
        System.out.println("sendMessageList>>>"+sendMessageList.size());
        model.addAttribute("sendMessageList",sendMessageList);
        model.addAttribute("pageUtil",pageUtil);
        model.addAttribute("reciver",reciver);
        return "sendmessage";
    }

    @RequestMapping("deleteMeg")
    public String deleteMeg(Model model,@RequestParam("mid") Integer mid){
        messageService.deleteSendMessage(mid);
        return "redirect:/message/sendmessage.html";
    }

    @RequestMapping("toDetail")
    public String toDetail(Model model,@RequestParam("mid") Integer mid){
        Message sendMessageById = messageService.getSendMessageById(mid);
        model.addAttribute("sendMessage",sendMessageById);
        return "messageDetail";
    }

    @RequestMapping("toSendMessage")
    public String toSendMessage(HttpServletRequest request,Model model){
        People people1 = (People) request.getSession().getAttribute("people");
        String oid = people1.getOid();
        int count = userService.getTeacherCount("");
        List<People> people = userService.getTeacherName(oid);
        model.addAttribute("people",people);
        return  "sendmes";
    }

    @RequestMapping("sendMessage")
    public String sendMessage(HttpServletRequest request,Message message){
        People people = (People) request.getSession().getAttribute("people");
        message.setMsender(people.getOid());
        message.setMsendername(people.getOname());
        message.setMtime(Utils.getDate());
        if(message.getMtype().equals("群发")){
            message.setMcount(0);
        }else{
            message.setMstate(0);
        }
        System.out.println(message);
        messageService.sendMessage(message);

        return "redirect:/message/sendmessage.html";
    }

    /***接收消息***/
    @RequestMapping("reciveMessage")
    public String reciveMessage(HttpServletRequest request,@RequestParam(defaultValue = "") String sender,@RequestParam(defaultValue = "1") Integer currentPage,Model model){
        People people = (People) request.getSession().getAttribute("people");
        String oid = people.getOid();
        Integer oposition = people.getOposition();
        int count = messageService.getNewMessageCount(oid, oposition);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setSize(5);
        pageUtil.setCurrentPage(currentPage);
        pageUtil.setTotal(count);
        pageUtil.totalNumPage();
        pageUtil.startPage();
        System.out.println(people);
        List<Message> reciverMessage = messageService.getReciverMessage(oid, oposition, sender, pageUtil.getStart(), pageUtil.getSize());
        System.out.println("reSize>>  "+reciverMessage.size());
        model.addAttribute("reciverMessageList",reciverMessage);
        model.addAttribute("pageUtil",pageUtil);
        model.addAttribute("sender",sender);
        return "recivemessage";
    }
    @RequestMapping("messageDetail/{mid}/{mtype}")
    public String messageDetail(@PathVariable("mid") Integer mid,Model model,@PathVariable("mtype") String mtype){
        Message reciveMessage = messageService.getSendMessageById(mid);
        if(mtype.equals("私发")){
            messageService.updateState(mid);
        }else if(mtype.equals("群发")){
            Integer mcount = reciveMessage.getMcount();
            mcount++;
            messageService.updateCount(mcount,mid);
        }
        reciveMessage = messageService.getSendMessageById(mid);
        model.addAttribute("reciveMessage",reciveMessage);
        return "reciveMessageDetail";
    }

}
